#include "accountfriendresponse.h"

namespace Account {
/**
 * @brief FriendResponse::Friend::Friend
 * @param userid
 * @param userName
 * @param displayName
 * @param avatar
 */
FriendResponse::Item::Item(int userId,
                           const QString &userName,
                           const QString &displayName,
                           const QString &avatar):
    m_userId(userId),
    m_userName(userName),
    m_displayName(displayName),
    m_avatar(avatar)
{

}

/**
 * @brief FriendResponse::FriendResponse
 * @param parent
 */
FriendResponse::FriendResponse(QObject *parent) :
    Network::JSONListResponse(parent)
{
}

FriendResponse::~FriendResponse()
{
    qDeleteAll(m_items);
}


Network::Response::Type FriendResponse::type() const
{
    return Network::Response::Friend;
}



void FriendResponse::parseList(const QVariantList &list)
{
    foreach (QVariant item, list) {
        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The \"map\" field isn't a map"));
            return;
        }

        QVariantMap map = item.toMap();
        if (!map.contains("uid")) {
            setError(Response::Property, "The \"uid\" value missing");
            return;
        }

        QVariant userId = map.value("uid");
        if (userId.type() != QVariant::Int) {
            setError(Response::Property, "The \"uid\" value isn't competitived");
            return;
        }

        if (!map.contains("urn")) {
            setError(Response::Property, "The \"urn\" value missing");
            return;
        }

        QVariant userName = map.value("urn");
        if (userName.type() != QVariant::String) {
            setError(Response::Property, "The \"urn\" value isn't competitived");
            return;
        }

        if (!map.contains("dpn")) {
            setError(Response::Property, "The \"dpn\" value is missing");
            return;
        }

        QVariant displayName = map.value("dpn");
        if (displayName.type() != QVariant::String) {
            setError(Response::Property, "The \"dpn\" value isn't competitived");
            return;
        }


        if (!map.contains("avt")) {
            setError(Response::Property, "The \"avt\" value is missing");
            return;
        }

        QVariant avatar = map.value("avt");
        if (avatar.type() != QVariant::String) {
            setError(Response::Property, "The \"avt\" value isn't competitived");
            return;
        }

        m_items.insert(userId.toInt(), new Item(userId.toInt(),
                                                    userName.toString(),
                                                    displayName.toString(),
                                                    avatar.toString()));
    }
}
} // namespace Account
